package org.itboys.ace.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.admin.entity.AdminMenu;
import org.itboys.admin.service.AdminMenuService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.AceJsonPageUtils;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 菜单管理
 * 
 * @author huanglei
 */
@RestController
@RequestMapping("/ace/admin/menu")
public class AceAdminMenuController extends BaseController {

	@Autowired
	private AdminMenuService adminMenuService;
	
	/**
	 * 跳转菜单列表页面
	 * 
	 * @return
	 */
	@RequestMapping("/menu-list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Model model) {
		return new ModelAndView("/ace/admin/menu-list");
	}

	/**
	 * 获取菜单列表
	 * 
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		Page<AdminMenu> page = adminMenuService.pageQuery(param, request);
		AceJsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}
	
	/***
	 * 根据id查询菜单信息
	 * 
	 * @param id
	 * @param response
	 */
	@RequestMapping("/getMenuById/{id}")
	public void getOrg(@PathVariable("id") Long id, HttpServletResponse response) {
		AjaxUtils.renderJson(response, adminMenuService.getById(id));
	}

	/**
	 * 新增/修改菜单
	 * 
	 * @param am
	 */
	@ResponseBody
	@RequestMapping(value = "/save")
	public void add(@ModelAttribute AdminMenu adminMenu, HttpServletResponse response) {
		try {
			if (adminMenu.getId() == 0l) {
				adminMenuService.save(adminMenu);
				if (adminMenu.getParentId() == 0) {
					adminMenu.setFullPath("/" + adminMenu.getId() + "/");
					adminMenu.setFullNamePath("/" + adminMenu.getMenuName() + "/");
				} else {
					AdminMenu parentMenu = adminMenuService.getById(adminMenu.getParentId());
					adminMenu.setFullPath("/" + parentMenu.getId() + "/" + adminMenu.getId() + "/");
					adminMenu.setFullNamePath("/" + parentMenu.getMenuName() + "/" + adminMenu.getMenuName() + "/");
				}
				adminMenuService.update(adminMenu);
			} else {
				if (adminMenu.getParentId() == 0) {
					AdminMenu temp = adminMenu;
					adminMenu = adminMenuService.getById(adminMenu.getId());
					adminMenu.setMenuName(temp.getMenuName());
					adminMenu.setMenuClass(temp.getMenuClass());
					adminMenu.setSort(temp.getSort());
					adminMenu.setUrl(temp.getUrl());
					adminMenu.setFullPath("/" + adminMenu.getId() + "/");
					adminMenu.setFullNamePath("/" + adminMenu.getMenuName() + "/");
				} else {
					AdminMenu parentMenu = adminMenuService.getById(adminMenu.getParentId());
					adminMenu.setFullPath("/" + parentMenu.getId() + "/" + adminMenu.getId() + "/");
					adminMenu.setFullNamePath("/" + parentMenu.getMenuName() + "/" + adminMenu.getMenuName() + "/");
				}
				adminMenuService.update(adminMenu);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("AdminMenu save error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	/**
	 * 根据菜单ID，删除一条数据
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public void delete(@PathVariable("id") Long id, HttpServletResponse response) {
		try {
			adminMenuService.deleteMenu(id);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("AdminMenu delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

}
